package com.example.shushiyunsst.fragment.main.wallet.sub

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.example.library_common.base.LazyFragment
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.wallet.bean.WalletHeadBean
import com.example.shushiyunsst.util.EncodingHandler
import com.example.wallet.bean.BalanceInfoBean
import com.example.wallet.db.UserInfoManager


/**
 * 收款
 * */
class CollectionsFragment : LazyFragment() {

    companion object {
        fun newInstance(address: String): CollectionsFragment {
            val args = Bundle()
            args.putString("address", address)
            val fragment = CollectionsFragment()
            fragment.arguments = args
            return fragment
        }

        fun newInstance(): CollectionsFragment {
            val args = Bundle()
            val fragment = CollectionsFragment()
            fragment.arguments = args
            return fragment
        }

        fun newInstance(item: BalanceInfoBean.ListBean): CollectionsFragment {
            val args = Bundle()
            args.putSerializable("item", item)
            val fragment = CollectionsFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var mCopyTv: TextView

    private lateinit var addressTv: TextView

    private lateinit var erc20Btn: TextView

    private lateinit var sstBtn: TextView

    private lateinit var mCodingImg: ImageView

    private var createQRCode: Bitmap? = null

    private var address: String = ""

    private var item: BalanceInfoBean.ListBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val userInfoWrapper = UserInfoManager.userInfoManager.getUserInfoWrapper()
        val sysWalletAddress = userInfoWrapper?.userInfo?.sysWalletAddress ?: ""

        val itemSerializable = arguments?.getSerializable("item")
        if (itemSerializable != null) {
            item = itemSerializable as BalanceInfoBean.ListBean
        }

        address = arguments?.getString("address", sysWalletAddress) ?: sysWalletAddress
    }

    override fun getLayoutId() = R.layout.collections_layout_fragment

    override fun lazyInit() {
        sstBtn.setOnClickListener {
            sstBtn.isSelected = true
            erc20Btn.isSelected = false
        }
        erc20Btn.setOnClickListener {
            sstBtn.isSelected = false
            erc20Btn.isSelected = true
        }

        mCopyTv.setOnClickListener {

            val clipboardManager: ClipboardManager =
                _mActivity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val clipData = ClipData.newPlainText("playerId", addressTv.text)
            clipboardManager.setPrimaryClip(clipData)

            Toast.makeText(_mActivity, "复制成功", Toast.LENGTH_SHORT).show()
        }

        sstBtn.performClick()

        addressTv.text = address

        if (address.isNotEmpty()) {
            try {
                createQRCode = EncodingHandler.createQRCode(address, PixUtils.dp2px(200))
                mCodingImg.setImageBitmap(createQRCode)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

    }

    override fun initView(view: View) {
        mCopyTv = view.findViewById(R.id.mCopyTv)
        addressTv = view.findViewById(R.id.addressTv)
        erc20Btn = view.findViewById(R.id.erc20Btn)
        sstBtn = view.findViewById(R.id.sstBtn)
        mCodingImg = view.findViewById(R.id.mCodingImg)

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)


        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        val mHeadFragment = when {
            (item != null) -> {
                val walletHeadBean = WalletHeadBean(
                    item?.balance,
                    item?.balanceCny,
                    item?.tokenType
                )
                WalletHeadFragment.newInstance(walletHeadBean)
            }
            else -> WalletHeadFragment.newInstance()
        }
        loadRootFragment(R.id.wallet_header, mHeadFragment)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (createQRCode?.isRecycled == false) {
            createQRCode?.recycle()
        }
    }
}